package com.bzs.batch.reconcile.batch.itemwriter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bzs.batch.reconcile.entity.ReconcileBatch;
import com.bzs.batch.reconcile.entity.SysTransactionflow;
import com.bzs.batch.reconcile.mapper.ReconcileBatchMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;


/**
 * @Classname SysToFileWriter
 * @Author baozs
 * @Date 2021/12/22 21:00
 * @Version 1.0
 * @Description
 **/
@Configuration
@AllArgsConstructor
public class SysToFileWriter {
    private final ReconcileBatchMapper reconcileBatchMapper;

    /**
     * @return FlatFileItemWriter<SysTransactionflow> 文件书写者
     * @throws Exception 异常处理
     */
    @Bean
    @StepScope
    public FlatFileItemWriter<SysTransactionflow> getFileWrite(
            @Value("#{jobParameters[date]}") String date,
            @Value("#{jobParameters[totalCount]}") String totalCount,
            @Value("#{jobParameters[totalAmount]}") String totalAmount
    ) throws Exception {
        //总笔数
        //总金额
        DelimitedLineAggregator<SysTransactionflow> dl = new DelimitedLineAggregator<>();
        dl.setDelimiter("");
        dl.setFieldExtractor(SysTranShort -> {
            SysTransactionflow SysTran = this.coverage(SysTranShort);
            String[] values = new String[7];
            values[0] = SysTran.getHostSerialno();//全局流水号
            values[1] = SysTran.getDebitAccount();//借方账号
            values[2] = SysTran.getCreditAccount();//贷方账号
            values[3] = SysTran.getTradeData();//核心日期
            values[4] = SysTran.getTradeTime();//核心时间
            values[5] = SysTran.getTradeResult();//交易结果
            values[6] = SysTran.getTransAmount();//交易金额
            return values;
        });
        FlatFileItemWriter<SysTransactionflow> writer = new FlatFileItemWriter<>();
        //LIQUH+8位日期+10位流水号.txt
        long timeMillis = System.currentTimeMillis();
        String filePath = "C:\\project\\progect_reconcil\\HOUSE\\LIQUH" + timeMillis + ".txt";
        writer.setResource(new FileSystemResource(filePath));
        writer.setLineAggregator(dl);
        writer.setEncoding("UTF-8");
        QueryWrapper<ReconcileBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_no", date);
        writer.setHeaderCallback(headerWriter -> headerWriter.write(totalAmount + "$" + totalCount));
        writer.afterPropertiesSet();
        writer.setLineSeparator("\n");
        return writer;
    }

    //给SysTransactionflow补位
    public SysTransactionflow coverage(SysTransactionflow sysTran) {
        if (sysTran.getHostSerialno().length() < 32) {
            sysTran.setHostSerialno(StringUtils.rightPad(sysTran.getHostSerialno(), 32, "*"));
        }
        if (sysTran.getDebitAccount().length() < 16) {
            sysTran.setDebitAccount(StringUtils.rightPad(sysTran.getDebitAccount(), 16, "*"));
        }
        if (sysTran.getCreditAccount().length() < 16) {
            sysTran.setCreditAccount(StringUtils.rightPad(sysTran.getCreditAccount(), 16, "*"));
        }
        if (sysTran.getTradeData().length() < 8) {
            sysTran.setTradeData(StringUtils.rightPad(sysTran.getTradeData(), 8, "*"));
        }
        if (sysTran.getTradeTime().length() < 8) {
            sysTran.setTradeTime(StringUtils.rightPad(sysTran.getTradeTime(), 8, "*"));
        }
        if (sysTran.getTradeResult().length() < 6) {
            sysTran.setTradeResult(StringUtils.rightPad(sysTran.getTradeResult(), 6, "*"));
        }
        return sysTran;
    }
}
